
package com.eshore.ezone.activities;

import com.eshore.ezone.R;
import com.eshore.ezone.view.Workspace;
import com.eshore.ezone.view.Workspace.OnScreenChangeListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

public class IntroductionActivity extends Activity {
    private static final int MSG_START = 0;
    private int mCurScreenIndex = 0;
    private Workspace mWorkspace;
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == MSG_START && mCurScreenIndex == 3) {
                Intent intent = new Intent(IntroductionActivity.this, AppDetailsActivity.class);
                startActivity(intent);
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.introduction);
        setupViews();
    }

    private void setupViews() {
        mWorkspace = (Workspace) findViewById(R.id.worksapce);
        mWorkspace.addView(getImageView(R.drawable.intr_img_01));
        mWorkspace.addView(getImageView(R.drawable.intr_img_02));
        mWorkspace.addView(getImageView(R.drawable.intr_img_03));
        mWorkspace.addView(getImageView(R.drawable.intr_img_04));
        mWorkspace.setOnScreenChangeListener(mScreenChangeListener);
    }

    private View getImageView(int id) {
        final ImageView imageView = new ImageView(this);
        final int width = ViewGroup.LayoutParams.FILL_PARENT;
        LayoutParams params = new ViewGroup.LayoutParams(width, width);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(id);
        return imageView;
    }
    
    private final OnScreenChangeListener mScreenChangeListener = new OnScreenChangeListener() {
        @Override
        public void onScreenChanged(View newScreen, int newScreenIndex) {
            mCurScreenIndex = newScreenIndex;
            if(newScreenIndex == 3) { //Delayed 1 second to launch the activity.
                mHandler.sendEmptyMessageDelayed(MSG_START, 1000);
            }
        }
        @Override
        public void onScreenChanging(View newScreen, int newScreenIndex) {}
    };
}
